/**
 * tabs - jQuery xui
 *
 * Licensed under the GPL: http://www.gnu.org/licenses/gpl.txt
 *
 * Copyright 2015 xjb [ beymy.en@gmail.com ]
 *
 */
(function($) {

    function bindEvents(target) {
        var t = $(target);
        var state = $.data(target, 'tabs');
        var opts = state.options;

        t.find('>li>a').unbind('.tabs').bind('click.tabs', function() {
        	var a = $(this);
            var pId = a.attr('href');
            var p = $(pId);
			
			a.parent().parent().find('>li').removeClass('active');
			p.parent().find('>div.tab-pane').removeClass('active');
			a.parent().addClass('active');
			p.addClass('active');
        });
    }

    $.fn.tabs = function(options, param) {
        if (typeof options == 'string') {
            return $.fn.tabs.methods[options](this, param);
        }

        options = options || {};

        return this.each(function() {
            var state = $.data(this, 'tabs');
            var opts;
            if (state) {
                opts = $.extend(state.options, options);
            } else {
                opts = $.extend({}, $.fn.tabs.defaults, $.fn.tabs.parseOptions(this), options);
                $.data(this, 'tabs', {
                    options: opts
                });
            }

            bindEvents(this);
        });
    };

    $.fn.tabs.methods = {};

    $.fn.tabs.parseOptions = function(target) {
        return $.extend({}, $.parser.parseOptions(target));
    };

    $.fn.tabs.defaults = {};
})(jQuery);